package com.isoftstone.studentregistration.service.Impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.isoftstone.studentregistration.mapper.UserMapper;
import com.isoftstone.studentregistration.pojo.Users;
import com.isoftstone.studentregistration.service.UserService;
import com.isoftstone.studentregistration.util.Result;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @version v1.0
 * @date 2025/9/7 10:14
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<?> insertUser(Users user) {
        Users users= userMapper.queryByPhone(user.getPhone());

        if (users == null) {
          user.setPassword(new String(SecureUtil.md5(user.getPassword())));
          user.setCreateTime(new Date());
          int insert = userMapper.insert(user);
          if (insert > 0) {
            return Result.success("注册成功");
          }
          return Result.error("注册失败");
        }
        return Result.error("手机号已存在");
    }

    @Override
    public Result<?> updateUser(Users user) {
        user.setUpdateTime(new Date());
        user.setPassword(new String(SecureUtil.md5(user.getPassword())));
        int result = userMapper.update(user);
        if (result > 0) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }

    @Override
    public Result<?> deleteUser(Integer id) {
        int result = userMapper.delete(id);
        if (result > 0) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @Override
    public com.github.pagehelper.Page queryAllStudents(Integer currentPage, Integer pageSize,Users queryCondition) {
        Page<Users> page = PageHelper.startPage(currentPage, pageSize);
        userMapper.queryAllStudents(queryCondition);
        return page;
    }

    @Override
    public Result<?> login(String phone, String password) {
        Users user=userMapper.login(phone,new String(SecureUtil.md5(password)));
        return user==null?Result.error("用户名或密码错误"):Result.success(user);

    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        List<Users> users=userMapper.queryAllStudents();
        ExcelWriter writer= ExcelUtil.getWriter(true);
        writer.write(users,true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName= null;
        try {
            fileName = URLEncoder.encode("用户信息","UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
        ServletOutputStream out= null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        writer.flush(out,true);
        writer.close();
        IoUtil.close(out);
    }


}
